function findAllGoodsAndComments() {
    $.ajax({
        url: '/findAllGoodsAndComments',
        type: 'post',
        dataType: "json",
        success:function(result){
            var list = result.data;
            $("#tbody").empty();
            for (var i = 0; i <list.length ; i++) {
                var id = list[i].gid;
                var goodsName = list[i].goodsName;
                var price = list[i].price;
                var caption = list[i].caption;
                var num = list[i].num;
                var comments = list[i].comments;
                var aa=0;
                for (var j = 0; j < comments.length; j++) {
                    var star = comments[j].star;
                    aa+=Number(star);
                }
                var bb=Number(aa/j).toFixed(1);
                var cc='星'
                var dd=bb+cc

                var $tr = '<tr>'+
                    '<th>'+goodsName+'</th>'+
                    '<th>'+price+'</th>'+
                    '<th>'+caption+'</th>'+
                    '<th>'+num+'</th>'+
                    '<th>'+dd+'</th>'+
                    '<th>'+' <button type="button" class="btn btn-success" data-toggle="modal" onclick="findName('+id+')" data-target="#myModal1">评论商品</button>' +
                    '<button type="button" class="btn btn-warning"  onclick="findOne('+id+')" data-toggle="modal" data-target="#myModal">查看评论</button>'+'</th>'+
                    '</tr>'
                $("#tbody").append($tr);
            }
        }
    })
}

function findOne(id) {
    var dd=id;
    document.getElementById("bottom-more-info-link").setAttribute("action","/findAllExport?gId="+id);
    $.ajax({
        url: '/findOne',
        type: 'post',
        dataType: "json",
        data:{
            gId:dd,
        },

       // action="/findAllExport?gId"

        success:function(result){
            var list = result.data;
            $(".modal-body").empty();

            for (var i = 0; i <list.length ; i++) {
                var comment = list[i].comment;
                var star = list[i].star;
                var cc='星'
                var dd=star+cc
                var $tr = '<div>'+
                    '<th>'+dd+'</th><br>'+
                    '<th>'+comment+'</th>'+
                    '</div>'
                $(".modal-body").append($tr);
            }
        }
    })
}

function findName(id) {
    var dd = id;
    $.ajax({
        url: '/findName',
        type: 'post',
        dataType: "json",
        data: {
            gId: dd,
        },
        success: function (result) {
            var data = result.data;
            $("#goodId").val(data);
        }
    })
}
function addComments() {
    var star = $("#user-name-label").val();
    var comment = $("#comment").val();
    var goodId = $("#goodId").val();
    $.ajax({
        url: '/addComment',
        type: 'post',
        dataType: "json",
        data: {
            star:star,
            comment:comment,
            goodId:goodId,
        },
        success: function (result) {
            var msg = result.msg;
          alert(msg)
        }
    })
}